package top.yunmouren.areamusic.event;


import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import top.yunmouren.areamusic.AreaMusic;
import top.yunmouren.areamusic.config.AreaDto;
import top.yunmouren.areamusic.exception.PlayerNullException;
import top.yunmouren.areamusic.music.MusicTag;
import top.yunmouren.areamusic.player.PlayerBridge;
import top.yunmouren.areamusic.utils.MusicUtil;

public class PlayerMoveListener extends MusicUtil implements Listener {
    private final AreaMusic plugin;

    public PlayerMoveListener(AreaMusic plugin) {
        super(plugin);
        this.plugin = plugin;
    }

    @EventHandler
    public void onEvent(PlayerMoveEvent event) {
        try {
            PlayerBridge playerBridge = new PlayerBridge(event.getPlayer(), this.plugin);
            Location playerToLocation = event.getTo();
            Location playerFromLocation = event.getFrom();
            if (playerToLocation == null) {
                return;
            }

            if (playerToLocation.getWorld() == null || playerFromLocation.getWorld() == null) {
                return;
            }

            if (playerToLocation.getWorld() == playerToLocation.getWorld() && playerToLocation.getBlockX() == playerFromLocation.getBlockX() && playerToLocation.getBlockY() == playerFromLocation.getBlockY() && playerToLocation.getBlockZ() == playerFromLocation.getBlockZ()) {
                return;
            }

            if (this.plugin.getConfigManager().getAreas().get(playerToLocation.getWorld().getName()) == null) {
                return;
            }

            (this.plugin.getConfigManager().getAreas().get(playerToLocation.getWorld().getName())).forEach((key, value) -> {
                if (value.getEnabled()) {
                    if (this.isInside(value, playerToLocation.getX(), playerToLocation.getY(), playerToLocation.getZ()) && !this.isInside(value, playerFromLocation.getX(), playerFromLocation.getY(), playerFromLocation.getZ())) {
                        this.checkAndPlay(playerBridge, value);
                    }

                    if (!this.isInside(value, playerToLocation.getX(), playerToLocation.getY(), playerToLocation.getZ()) && this.isInside(value, playerFromLocation.getX(), playerFromLocation.getY(), playerFromLocation.getZ())) {
                        this.checkAndTerminate(playerBridge, value);
                    }

                }
            });
        } catch (PlayerNullException ignored) {
        }

    }

    private void checkAndPlay(PlayerBridge playerBridge, AreaDto currentDto) {
        this.freshTag(playerBridge);
        MusicTag musicTag = playerBridge.getPlayerMusicTag();
        if (musicTag == null) {
            this.play(playerBridge, currentDto);
        } else if (currentDto.getPriority().getValue() > musicTag.getPriority().getValue() && currentDto.getOverWrite()) {
            playerBridge.getPlayer().stopSound(musicTag.getMusicURL());
            this.play(playerBridge, currentDto);
        }

    }

    private void checkAndTerminate(PlayerBridge playerBridge, AreaDto currentDto) {
        this.freshTag(playerBridge);
        MusicTag musicTag = playerBridge.getPlayerMusicTag();
        if (musicTag != null && musicTag.getAreaUUID().equals(currentDto.getUuid())) {
            playerBridge.clearPlayerMusicTag();
            playerBridge.getPlayer().stopSound(musicTag.getMusicURL());
        }

    }
}
